<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>module RDoc::Encoding - rdoc: Ruby Standard Library Documentation</title>


<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>

<script src="../js/jquery-3.2.0.min.js"></script>

<script src="../js/vue.min.js"></script>
<script src="../js/js.cookie.min.js"></script>

<link href="../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../css/rdoc.css" rel="stylesheet">
<link href="../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
  var index_rel_prefix = "../";
  var darkModeCsseHref = "../css/rdoc-dm.css"
  var defaultModeCssHref = "../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="module">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../table_of_contents.html#pages">Pages</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    

    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-change_encoding">::change_encoding</a>
    <li ><a href="#method-c-detect_encoding">::detect_encoding</a>
    <li ><a href="#method-c-read_file">::read_file</a>
    <li ><a href="#method-c-remove_frozen_string_literal">::remove_frozen_string_literal</a>
    <li ><a href="#method-c-remove_magic_comment">::remove_magic_comment</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="module-RDoc::Encoding">
    <h1 id="module-RDoc::Encoding" class="module">
      module RDoc::Encoding
    </h1>

    <section class="description">
    
<p>This class is a wrapper around File IO and <a href="Encoding.html"><code>Encoding</code></a> that helps <a href="RDoc.html"><code>RDoc</code></a> load files and convert them to the correct encoding.</p>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">





                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-change_encoding" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">change_encoding</span><span
                                class="method-args">(text, encoding)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Changes encoding based on <code>encoding</code> without converting and returns new string</p>

                              <div class="method-source-code" id="change_encoding-source">
            <pre><span class="ruby-comment"># File rdoc/encoding.rb, line 123</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">change_encoding</span> <span class="ruby-identifier">text</span>, <span class="ruby-identifier">encoding</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">text</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">RDoc</span><span class="ruby-operator">::</span><span class="ruby-constant">Comment</span>
    <span class="ruby-identifier">text</span>.<span class="ruby-identifier">encode!</span> <span class="ruby-identifier">encoding</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-constant">String</span>.<span class="ruby-identifier">new</span> <span class="ruby-identifier">text</span>, <span class="ruby-value">encoding:</span> <span class="ruby-identifier">encoding</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-detect_encoding" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">detect_encoding</span><span
                                class="method-args">(string)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Detects the encoding of <code>string</code> based on the magic comment</p>

                              <div class="method-source-code" id="detect_encoding-source">
            <pre><span class="ruby-comment"># File rdoc/encoding.rb, line 103</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">detect_encoding</span> <span class="ruby-identifier">string</span>
  <span class="ruby-identifier">result</span> = <span class="ruby-constant">HEADER_REGEXP</span>.<span class="ruby-identifier">match</span> <span class="ruby-identifier">string</span>
  <span class="ruby-identifier">name</span> = <span class="ruby-identifier">result</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">result</span>[<span class="ruby-value">:name</span>]

  <span class="ruby-identifier">name</span> <span class="ruby-operator">?</span> <span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">name</span>) <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-read_file" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">read_file</span><span
                                class="method-args">(filename, encoding, force_transcode = false)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Reads the contents of <code>filename</code> and handles any encoding directives in the file.</p>

<p>The content will be converted to the <code>encoding</code>.  If the file cannot be converted a warning will be printed and nil will be returned.</p>

<p>If <code>force_transcode</code> is true the document will be transcoded and any unknown character in the target encoding will be replaced with ‘?’</p>

                              <div class="method-source-code" id="read_file-source">
            <pre><span class="ruby-comment"># File rdoc/encoding.rb, line 32</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">read_file</span> <span class="ruby-identifier">filename</span>, <span class="ruby-identifier">encoding</span>, <span class="ruby-identifier">force_transcode</span> = <span class="ruby-keyword">false</span>
  <span class="ruby-identifier">content</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span> <span class="ruby-identifier">filename</span>, <span class="ruby-string">&quot;rb&quot;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">read</span> <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">content</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-string">&quot;\r\n&quot;</span>, <span class="ruby-string">&quot;\n&quot;</span>) <span class="ruby-keyword">if</span> <span class="ruby-constant">RUBY_PLATFORM</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/mswin|mingw/</span>

  <span class="ruby-identifier">utf8</span> = <span class="ruby-identifier">content</span>.<span class="ruby-identifier">sub!</span>(<span class="ruby-regexp">/\A\xef\xbb\xbf/</span>, <span class="ruby-string">&#39;&#39;</span>)

  <span class="ruby-identifier">enc</span> = <span class="ruby-constant">RDoc</span><span class="ruby-operator">::</span><span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">detect_encoding</span> <span class="ruby-identifier">content</span>
  <span class="ruby-identifier">content</span> = <span class="ruby-constant">RDoc</span><span class="ruby-operator">::</span><span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">change_encoding</span> <span class="ruby-identifier">content</span>, <span class="ruby-identifier">enc</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">enc</span>

  <span class="ruby-keyword">begin</span>
    <span class="ruby-identifier">encoding</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">default_external</span>
    <span class="ruby-identifier">orig_encoding</span> = <span class="ruby-identifier">content</span>.<span class="ruby-identifier">encoding</span>

    <span class="ruby-keyword">if</span> <span class="ruby-keyword">not</span> <span class="ruby-identifier">orig_encoding</span>.<span class="ruby-identifier">ascii_compatible?</span> <span class="ruby-keyword">then</span>
      <span class="ruby-identifier">content</span> = <span class="ruby-identifier">content</span>.<span class="ruby-identifier">encode</span> <span class="ruby-identifier">encoding</span>
    <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">utf8</span> <span class="ruby-keyword">then</span>
      <span class="ruby-identifier">content</span> = <span class="ruby-constant">RDoc</span><span class="ruby-operator">::</span><span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">change_encoding</span> <span class="ruby-identifier">content</span>, <span class="ruby-constant">Encoding</span><span class="ruby-operator">::</span><span class="ruby-constant">UTF_8</span>
      <span class="ruby-identifier">content</span> = <span class="ruby-identifier">content</span>.<span class="ruby-identifier">encode</span> <span class="ruby-identifier">encoding</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-comment"># assume the content is in our output encoding</span>
      <span class="ruby-identifier">content</span> = <span class="ruby-constant">RDoc</span><span class="ruby-operator">::</span><span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">change_encoding</span> <span class="ruby-identifier">content</span>, <span class="ruby-identifier">encoding</span>
    <span class="ruby-keyword">end</span>

    <span class="ruby-keyword">unless</span> <span class="ruby-identifier">content</span>.<span class="ruby-identifier">valid_encoding?</span> <span class="ruby-keyword">then</span>
      <span class="ruby-comment"># revert and try to transcode</span>
      <span class="ruby-identifier">content</span> = <span class="ruby-constant">RDoc</span><span class="ruby-operator">::</span><span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">change_encoding</span> <span class="ruby-identifier">content</span>, <span class="ruby-identifier">orig_encoding</span>
      <span class="ruby-identifier">content</span> = <span class="ruby-identifier">content</span>.<span class="ruby-identifier">encode</span> <span class="ruby-identifier">encoding</span>
    <span class="ruby-keyword">end</span>

    <span class="ruby-keyword">unless</span> <span class="ruby-identifier">content</span>.<span class="ruby-identifier">valid_encoding?</span> <span class="ruby-keyword">then</span>
      <span class="ruby-identifier">warn</span> <span class="ruby-node">&quot;unable to convert #{filename} to #{encoding}, skipping&quot;</span>
      <span class="ruby-identifier">content</span> = <span class="ruby-keyword">nil</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Encoding</span><span class="ruby-operator">::</span><span class="ruby-constant">InvalidByteSequenceError</span>,
         <span class="ruby-constant">Encoding</span><span class="ruby-operator">::</span><span class="ruby-constant">UndefinedConversionError</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-identifier">e</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">force_transcode</span> <span class="ruby-keyword">then</span>
      <span class="ruby-identifier">content</span> = <span class="ruby-constant">RDoc</span><span class="ruby-operator">::</span><span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">change_encoding</span> <span class="ruby-identifier">content</span>, <span class="ruby-identifier">orig_encoding</span>
      <span class="ruby-identifier">content</span> = <span class="ruby-identifier">content</span>.<span class="ruby-identifier">encode</span>(<span class="ruby-identifier">encoding</span>,
                               <span class="ruby-value">:invalid</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-value">:replace</span>,
                               <span class="ruby-value">:undef</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-value">:replace</span>,
                               <span class="ruby-value">:replace</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&#39;?&#39;</span>)
      <span class="ruby-keyword">return</span> <span class="ruby-identifier">content</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">warn</span> <span class="ruby-node">&quot;unable to convert #{e.message} for #{filename}, skipping&quot;</span>
      <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">content</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">ArgumentError</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-identifier">e</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">message</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/unknown encoding name - (.*)/</span>
  <span class="ruby-identifier">warn</span> <span class="ruby-node">&quot;unknown encoding name \&quot;#{$1}\&quot; for #{filename}, skipping&quot;</span>
  <span class="ruby-keyword">nil</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">EISDIR</span>, <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">ENOENT</span>
  <span class="ruby-keyword">nil</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-remove_frozen_string_literal" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">remove_frozen_string_literal</span><span
                                class="method-args">(string)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="remove_frozen_string_literal-source">
            <pre><span class="ruby-comment"># File rdoc/encoding.rb, line 89</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">remove_frozen_string_literal</span> <span class="ruby-identifier">string</span>
  <span class="ruby-identifier">string</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/\A(?:#!.*\n)?(.*\n)/</span>
  <span class="ruby-identifier">first_line</span> = <span class="ruby-node">$1</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">first_line</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/\A# +frozen[-_]string[-_]literal[=:].+$/i</span>
    <span class="ruby-identifier">string</span> = <span class="ruby-identifier">string</span>.<span class="ruby-identifier">sub</span> <span class="ruby-identifier">first_line</span>, <span class="ruby-string">&#39;&#39;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">string</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-remove_magic_comment" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">remove_magic_comment</span><span
                                class="method-args">(string)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Removes magic comments and shebang</p>

                              <div class="method-source-code" id="remove_magic_comment-source">
            <pre><span class="ruby-comment"># File rdoc/encoding.rb, line 113</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">remove_magic_comment</span> <span class="ruby-identifier">string</span>
  <span class="ruby-identifier">string</span>.<span class="ruby-identifier">sub</span> <span class="ruby-constant">HEADER_REGEXP</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">s</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp">/[^\n]/</span>, <span class="ruby-string">&#39;&#39;</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>. </p><p><a href="https://jamesbritt.bandcamp.com/">Hack your world.  Feed your head.  Live curious</a>.</p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

